/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.NoMaterialsDao;
import me.zhengjie.dao.OutBoundTaskDao;
import me.zhengjie.server.OutBoundTaskService;
import me.zhengjie.utils.PageResult;

import me.zhengjie.vo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hws
* @date 2025-06-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "outboundTask管理")
@RequestMapping("/api/outBoundTask")
public class OutBoundTaskController {

    private final OutBoundTaskService outBoundTaskService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('outBoundTask:list')")
    public void exportOutBoundTask(HttpServletResponse response, OutBoundTaskDao criteria) throws IOException {
        outBoundTaskService.download(outBoundTaskService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询outboundTask")
    @ApiOperation("查询outboundTask")
    @PreAuthorize("@el.check('outBoundTask:list')")
    public ResponseEntity<PageResult<OutBoundTaskDao>> queryOutBoundTask(OutBoundTaskDao criteria, Page<OutBoundTaskDao> page){
        return new ResponseEntity<>(outBoundTaskService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增outboundTask")
    @ApiOperation("新增outboundTask")
    public ResponseEntity<Object> createOutBoundTask(@Validated @RequestBody OutBoundTaskDao resources){
        outBoundTaskService.add(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改outboundTask")
    @ApiOperation("修改outboundTask")
    public ResponseEntity<Object> updateOutBoundTask(@Validated @RequestBody OutBoundTaskDao resources){
        outBoundTaskService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("取消outboundTask")
    @ApiOperation("取消outboundTask")
    public ResponseEntity<Object> deleteOutBoundTask(@Validated @RequestBody OutBoundTaskDao resources) {
        resources.setStatus(10);
        outBoundTaskService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/start")
    @Log("开始outboundTask")
    @ApiOperation("开始任务outboundTask")
    public ResponseEntity<Object> startOutBoundTask(@RequestBody OutBoundTaskDao outBoundTaskDao) throws InterruptedException {
        outBoundTaskService.startOutBoundTask(outBoundTaskDao);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/sorted")
    @Log("出库分拣")
    @ApiOperation("出库分拣")
    public Result<Object> outboundSort(String outBoundCode, String containerCode, String code, Double weight) throws IOException {
        return Result.success(outBoundTaskService.outboundSort(outBoundCode,containerCode,code,weight));
    }



    @GetMapping("/lack")
    @Log("报缺")
    @ApiOperation("报缺")
    public void lack(String materialCode,String taskId,Double weight)  {
        outBoundTaskService.lack(materialCode,taskId,weight);
    }
}